package com.touchwisdom.fetch.admin.common;

import org.eclipse.jetty.webapp.ClasspathPattern;

import com.jfinal.autotable.AutoTableActiveRecordPlugin;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.hikaricp.HikariCpPlugin;
import com.jfinal.template.Engine;
import com.jfinal.template.source.ClassPathSource;
import com.touchwisdom.fetch.admin.web.IndexController;
import com.touchwisdom.fetch.core.model.TDbSource;
import com.touchwisdom.fetch.core.model.TFetchAppender;
import com.touchwisdom.fetch.core.model.TFetchConfig;
import com.touchwisdom.fetch.core.model.TFileFetch;
import com.touchwisdom.fetch.core.model.TFixedField;
import com.touchwisdom.fetch.core.web.FetchAppenderController;
import com.touchwisdom.fetch.core.web.FetchDbSourceController;
import com.touchwisdom.fetch.core.web.FetchSystemConfigController;
import com.touchwisdom.fetch.core.web.FileFetchController;
import com.touchwisdom.fetch.core.web.FixedFieldController;
import com.touchwisdom.modular.common.CommonConfigKit;
import com.touchwisdom.modular.common.model.SysEntity;
import com.touchwisdom.modular.common.model.SysEntityGrid;

/**
 * 抓取管理端的第一端
 * 
 * @filename FetchAdminConfig.java
 * @author ggeagle
 * @datetime 2017年9月21日 上午10:31:40
 */
public class FetchAdminConfig extends JFinalConfig {
	@Override
	public void configConstant(Constants me) {
		PropKit.use("application.properties");
		me.setDevMode(PropKit.getBoolean("devMode", true));
	}

	@Override
	public void configRoute(Routes me) {
		me.setBaseViewPath("WEB-INF/content");
		me.add("/", IndexController.class, "/index");

		me.add("/fetch/appender", FetchAppenderController.class);
		me.add("/fetch/config", FetchSystemConfigController.class);
		me.add("/fetch/dbsource", FetchDbSourceController.class);
		me.add("/fixed", FixedFieldController.class);

		me.add("/file", FileFetchController.class);
	}

	@Override
	public void configEngine(Engine me) {
		CommonConfigKit.configCommonEngine(me);
		//抓取器相关的类
		me.addSharedFunction(new ClassPathSource("/templates/fetch/config/_form.html"));
	}

	@Override
	public void configPlugin(Plugins me) {
		// DB plugin
		HikariCpPlugin hikariCp = new HikariCpPlugin(String.format(PropKit.get("jdbcUrl"), PathKit.getWebRootPath()),
				PropKit.get("username"), PropKit.get("password").trim());
		// 加载sqlite的驱动,其他操作和mysql的一样
		hikariCp.setDriverClass(PropKit.get("driverClass"));
		me.add(hikariCp);
		AutoTableActiveRecordPlugin arp = new AutoTableActiveRecordPlugin(hikariCp);
		arp.addMapping(SysEntity.class);
		arp.addMapping(SysEntityGrid.class);
		arp.addMapping(TFetchAppender.class);
		arp.addMapping(TDbSource.class);
		arp.addMapping(TFetchConfig.class);
		arp.addMapping(TFixedField.class);

		arp.addMapping(TFileFetch.class);
		me.add(arp);
	}

	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub
	}

	@Override
	public void configHandler(Handlers me) {
		CommonConfigKit.configCommonHandler(me);
	}
}